package com.zhku.shoppingspringboot.common;

import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：zjy
 * 时间：2024/8/24 17:16
 * 描述:无bug
 */
//自定义的RedisCache加入到RedisCacheManager
public class RedisCacheManagerResolver extends RedisCacheManager {

    private final RedisCacheWriter cacheWriter;
    private final RedisCacheConfiguration defaultCacheConfig;

    public RedisCacheManagerResolver(RedisCacheWriter cacheWriter,
                                     RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }

    public RedisCacheManagerResolver(RedisCacheWriter cacheWriter,
                                     RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheNames);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }

    public RedisCacheManagerResolver(RedisCacheWriter cacheWriter,
                                     RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation,
                                     String... initialCacheNames) {
        super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }

    public RedisCacheManagerResolver(RedisCacheWriter cacheWriter,
                                     RedisCacheConfiguration defaultCacheConfiguration,
                                     Map<String, RedisCacheConfiguration> initialCacheConfigurations) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }

    public RedisCacheManagerResolver(RedisCacheWriter cacheWriter,
                                     RedisCacheConfiguration defaultCacheConfiguration,
                                     Map<String, RedisCacheConfiguration> initialCacheConfigurations,
                                     boolean allowInFlightCacheCreation) {
        super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations,
                allowInFlightCacheCreation);
        this.cacheWriter = cacheWriter;
        this.defaultCacheConfig = defaultCacheConfiguration;
    }

    public RedisCacheManagerResolver(RedisConnectionFactory redisConnectionFactory,
                                     RedisCacheConfiguration cacheConfiguration) {
        this(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), cacheConfiguration);
    }


    @Override
    protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
        return new RedisCacheResolver(name, cacheWriter,
                cacheConfig != null ? cacheConfig : defaultCacheConfig);
    }

    @Override
    public Map<String, RedisCacheConfiguration> getCacheConfigurations() {
        Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(getCacheNames().size());
        getCacheNames().forEach(name -> {
            RedisCache cache = RedisCacheResolver.class.cast(lookupCache(name));
            configurationMap.put(name, cache != null ? cache.getCacheConfiguration() : null);
        });
        return Collections.unmodifiableMap(configurationMap);
    }
}